from model_config import MODEL_PROVIDERS
from llm.providers.ollama_provider import OllamaProvider
from llm.providers.openai_provider import OpenAIProvider

class ProviderFactory:
    # 注册映射：type -> 实现类
    _provider_map = {
        "ollama": OllamaProvider,
        "openai": OpenAIProvider,
    }

    @staticmethod
    def get_provider(provider_name: str):
        """
        根据 provider_name (配置里的 key) 返回 Provider 实例
        """
        cfg = MODEL_PROVIDERS.get(provider_name)
        if not cfg:
            raise ValueError(f"Provider {provider_name} not found in config")

        provider_type = cfg.get("type")
        if not provider_type:
            raise ValueError(f"Provider {provider_name} 缺少 'type' 字段")

        provider_cls = ProviderFactory._provider_map.get(provider_type)
        if not provider_cls:
            raise ValueError(f"No implementation for provider type {provider_type}")

        return provider_cls(
            base_url=cfg.get("base_url"),
            api_key=cfg.get("api_key"),
            timeout=cfg.get("timeout", 60),
        )
